package com.zktr.dao;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.zktr.bean.AdminBean;
import com.zktr.pojo.Admin;
import com.zktr.pojo.Goods;
import com.zktr.utils.CRUDDAO;

public class AdminDao {
	CRUDDAO<Admin> crud = new CRUDDAO<>(Admin.class);

	public static void main(String[] args) {
		AdminDao adminDao = new AdminDao();
		Date date = new Date();
		 long gmtMills = date.getTime() + 60000 * date.getTimezoneOffset();

		    int a=(int) (gmtMills / 1000l);
		System.out.println(a);
		Admin admin=new Admin();
	
		admin.setUsername("张章天");
		admin.setUserid(a);
		admin.setEmail("18397489@qq.com");
		admin.setPassword("123456");
		admin.setAddtime(1281691245);
		admin.setLastlogin(1341281600);
		admin.setRolelist("京东hr");
		admin.setRoleid(1);
		admin.setPortrait("1111");
		System.out.println(adminDao.insertOneAdmin(admin));
	/*	AdminBean adminBean = new AdminBean();
		adminBean.setEmail("123456qq@.com");
		adminBean.setUsername("刘强东");
		adminBean.setPassword("123456");
		adminBean.setPortrait("../img/lqd.jpg");
		System.out.println(adminDao.updateGoods(adminBean, "1"));*/
	}



	/**
	 * 查询所有管理员个人信息
	 * @return map
	 */
	public  List<Admin> queryAllAdmin() {
		Map<String, Object[]> map = new LinkedHashMap<String, Object[]>();
		String sql = "select * from admin_user";
		map.put(sql, null);
		return crud.selectAll(map).get(0);

	}
	/**
	 * 登录验证
	 * 用户名、密码
	 * @return
	 */
	public List<Admin> loginVerification(String username,String password) {
		Map<String, Object[]> map = new LinkedHashMap<String, Object[]>();
		Object object[] = {username,password};
		String sql = "select * from admin_user where username=? and password=? ";
		map.put(sql, object);
		return crud.selectAll(map).get(0);
	}
	public List<Admin> loginVerificationUsername(String username) {
		Map<String, Object[]> map = new LinkedHashMap<String, Object[]>();
		Object object[] = {username};
		String sql = "select * from admin_user where username=?";
		map.put(sql, object);
		return crud.selectAll(map).get(0);
	}
	/**
	 * 新增管理员信息
	 * @param goods
	 * @return
	 */
	public int insertOneAdmin(Admin admin) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("insert into admin_user values(?,?,?,?,?,?,?,?,?)", 
				new Object[]{admin.getUserid(),admin.getUsername(),admin.getEmail(),admin.getPassword(),admin.getAddtime(),admin.getLastlogin(),
						admin.getRolelist(),admin.getRoleid(),admin.getPortrait()});
		 return crud.updateAll(map)[0];
	}
	/**
	 * 更新管理员信息
	 * @param goods
	 * @param id
	 * @return
	 */
	public int updateGoods(AdminBean adBean,String id) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("update admin_user set  username=?,email=? where userid=?", 
				new Object[]{adBean.getUsername(),adBean.getEmail(),id});
		 return crud.updateAll(map)[0];
	}
	/**
	 * 更新管理员信息
	 * @param goods
	 * @param id
	 * @return
	 */
	public int updateAdmin(AdminBean adBean,String id) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("update admin_user set portrait=? where userid=?", 
				new Object[]{adBean.getPortrait(),id});
		 return crud.updateAll(map)[0];
	}
	
	/**
	 * 根据管理员id删除管理员
	 * @param id
	 * @return
	 */
	public int deleteOneAdmin(int id) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("delete from admin_user where userid=? ",new Object[]{id});
		 return crud.updateAll(map)[0];
		
	}
	
	
	
}
